<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="150px"
    >
      <el-form-item label="老人" prop="oldManName">
        <!-- <el-select v-model="form.oldManId" @change="oldChange" filterable placeholder="请选择老人">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select> -->
        <el-input
          v-model="queryParams.oldManName"
          placeholder="请输入老人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总胆固醇(TCHO) " prop="cholesterol">
        <el-input
          v-model="queryParams.cholesterol"
          placeholder="请输入总胆固醇(TCHO) "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="甘油三酯(TG) " prop="triglyceride">
        <el-input
          v-model="queryParams.triglyceride"
          placeholder="请输入甘油三酯(TG) "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="低密度脂蛋白(LDL) " prop="lowDensityLipoprotein">
        <el-input
          v-model="queryParams.lowDensityLipoprotein"
          placeholder="请输入低密度脂蛋白(LDL) "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高密度脂蛋白(HDL)" prop="highDensityLipoprotein">
        <el-input
          v-model="queryParams.highDensityLipoprotein"
          placeholder="请输入高密度脂蛋白(HDL)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测时间" prop="detectionDate">
        <el-date-picker
            clearable
            v-model="queryParams.detectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择检测时间"
          >
          </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system :fat:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system :fat:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system :fat:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system :fat:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老人" align="center" prop="oldManName" />
      <el-table-column label="总胆固醇(TCHO) " align="center" prop="cholesterol" />
      <el-table-column label="甘油三酯(TG) " align="center" prop="triglyceride" />
      <el-table-column
        label="低密度脂蛋白(LDL) "
        align="center"
        prop="lowDensityLipoprotein"
      />
      <el-table-column
        label="高密度脂蛋白(HDL)"
        align="center"
        prop="highDensityLipoprotein"
      />
      <el-table-column label="检测时间" align="center" prop="detectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.detectionDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system :fat:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system :fat:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改血脂数据录入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="老人姓名" prop="oldManId">
          <el-select v-model="form.oldManId" filterable placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="总胆固醇(TCHO) " prop="cholesterol">
          <el-input v-model="form.cholesterol" type="number" placeholder="请输入总胆固醇(TCHO) " />
        </el-form-item>
        <el-form-item label="甘油三酯(TG) " prop="triglyceride">
          <el-input v-model="form.triglyceride" type="number" placeholder="请输入甘油三酯(TG) " />
        </el-form-item>
        <el-form-item label="低密度脂蛋白(LDL) " prop="lowDensityLipoprotein">
          <el-input
            v-model="form.lowDensityLipoprotein"
            type="number"
            placeholder="请输入低密度脂蛋白(LDL) "
          />
        </el-form-item>
        <el-form-item label="高密度脂蛋白(HDL)" prop="highDensityLipoprotein">
          <el-input
            v-model="form.highDensityLipoprotein"
            type="number"
            placeholder="请输入高密度脂蛋白(HDL)"
          />
        </el-form-item>
        <el-form-item label="检测时间" prop="detectionDate">
          <el-date-picker
            clearable
            v-model="form.detectionDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择检测时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFat, getFat, delFat, addFat, updateFat } from "@/api/system/fat";
import { listUser } from "@/api/system/users";
export default {
  name: "Fat",
  data() {
    return {
      userList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 血脂数据录入表格数据
      fatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oldManId: null,
        cholesterol: null,
        triglyceride: null,
        lowDensityLipoprotein: null,
        highDensityLipoprotein: null,
        dataSources: null,
        detectionDate: null,
        oldManName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        oldManId: [{ required: true, message: "老人不能为空", trigger: "change" }],
        cholesterol: [{ required: true, message: "总胆固醇不能为空", trigger: "blur" }],
        triglyceride: [{ required: true, message: "甘油三酯不能为空", trigger: "blur" }],
        lowDensityLipoprotein: [{ required: true, message: "低密度脂蛋白不能为空", trigger: "blur" }],
        highDensityLipoprotein: [{ required: true, message: "高密度脂蛋白能为空", trigger: "blur" }],
        detectionDate: [{ required: true, message: "检测时间不能为空", trigger: "change" }],
      },
    };
  },
  created() {
    this.getUserList();
    this.getList();
  },
  methods: {
    oldChange(e){
      console.log(e,'eeeeeeeeeeeeeeeeeeeeeeeeee');
    },
    /** 查询老人用户信息列表 */
    getUserList() {
      let obj = {
        pageNum: 1,
        pageSize: 10000,
      };
      listUser(obj).then((response) => {
        this.userList = response.rows;
      });
    },
    /** 查询血脂数据录入列表 */
    getList() {
      this.loading = true;
      listFat(this.queryParams).then((response) => {
        this.fatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        oldManId: null,
        cholesterol: null,
        triglyceride: null,
        lowDensityLipoprotein: null,
        highDensityLipoprotein: null,
        dataSources: null,
        detectionDate: null,
        oldManName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加血脂数据录入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFat(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改血脂数据录入";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFat(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFat(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除血脂数据录入编号为"' + ids + '"的数据项？')
        .then(function () {
          return delFat(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/fat/export",
        {
          ...this.queryParams,
        },
        `fat_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
